package com.itheima.web;

import cn.hutool.db.PageResult;
import com.itheima.servcie.SmallVideosService;
import com.itheima.vo.mongo.AllVo;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/smallVideos")
public class SmallVideosController {
    @Autowired
    private SmallVideosService smallVideosService;

    /**
     * 小视频列表
     * @param page
     * @param pagesize
     * @return
     */
    @Cacheable(value = "smallVideosList", key = "#page + '_' + #pagesize")
    @GetMapping
    public AllVo smallVideosList(@RequestParam(defaultValue = "1") Integer page,
                                      @RequestParam(defaultValue = "10") Integer pagesize){
        return smallVideosService.getSmallVideosList(page, pagesize);
    }

    /**
     * 上传视频
     * @param videoThumbnail
     * @param videoFile
     * @return
     */
    @CacheEvict(value = "smallVideosList", allEntries = true)
    @PostMapping
    public ResponseEntity upLoadVideo(MultipartFile videoThumbnail, MultipartFile videoFile){
        smallVideosService.upLoadVideo(videoThumbnail, videoFile);
        return ResponseEntity.status(HttpStatus.OK).body(null);
    }

    /**
     * 视频点赞
     * @param id
     * @return
     */
    @PostMapping("/{id}/like")
    public ResponseEntity like(@PathVariable String id){
        smallVideosService.like(id);
        return ResponseEntity.ok(null);
    }

    /**
     * 取消点赞
     * @param id
     * @return
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable String id){
        smallVideosService.dislike(id);
        return ResponseEntity.ok(null);
    }

    /**
     * 关注
     * @param uid
     * @return
     */
    @PostMapping("/{uid}/userFocus")
    public ResponseEntity userFocus(@PathVariable Long uid){
        smallVideosService.userFocus(uid);
        return ResponseEntity.ok(null);
    }

    /**
     * 取消关注
     * @param uid
     * @return
     */
    @PostMapping("/{uid}/userUnFocus")
    public ResponseEntity userUnFocus(@PathVariable Long uid){
        smallVideosService.userUnFocus(uid);
        return ResponseEntity.ok(null);
    }

    /**
     * 评论
     * @param id
     * @return
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity comments(@PathVariable String id,
                                   @RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize){
        return ResponseEntity.ok(smallVideosService.comments(page, pagesize,id));
    }

    /**
     * 发布评论
     * @param id
     * @return
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity comments(@PathVariable String id,
                                   @RequestBody Map<String, String> map){
        smallVideosService.pushComments(id, map);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频评论点赞
     * @param id
     * @return
     */
    @PostMapping("/comments/{id}/like")
    public ResponseEntity commentLike(@PathVariable ObjectId id){
        smallVideosService.commentLike(id);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频评论取消点赞
     * @param id
     * @return
     */
    @PostMapping("/comments/{id}/dislike")
    public ResponseEntity commentDislike(@PathVariable ObjectId id){
        smallVideosService.commentDislike(id);
        return ResponseEntity.ok(null);
    }
}
